package service.impl;

import context.LoginUserCache;
import dao.RoleDAO;
import dao.UserDAO;
import dao.UserRuleDAO;
import entity.User;
import entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.UserService;
import service.entity.LimitUser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by asus on 2017/6/17.
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;
    @Autowired
    private UserRuleDAO userRuleDAO;
    @Autowired
    private RoleDAO roleDAO;

    @Override
    public void addUser(User user) {
        userDAO.save(user);
    }

    @Override
    public void updateUser(User user) {
        userDAO.update(user);
    }

    @Transactional
    @Override
    public void deleteUserById(Long id) throws Exception{
        userDAO.deleteById(id);
        List<UserRole> userRoles = userRuleDAO.findUserRoleByUserId(id);
        for(UserRole ur:userRoles){
            userRuleDAO.delete(ur.getId());
        }
    }

    @Override
    public User getUser(String name, String pwd) {
        return userDAO.findUser(name, pwd);
    }

    @Override
    public Collection<User> getUsers(int page, int size) {
        return userDAO.fingPage(page, size);
    }

    @Override
    public Collection<User> getUsers(Collection<Long> ids) {
        return userDAO.findByIds(ids);
    }

    /**
     * 分页查询用户角色对应关系
     *
     * @param page
     * @param size
     * @return 用户角色对应关系集合
     */
    @Override
    public List<UserRole> getUserRoles(Integer page, Integer size) {
        return userRuleDAO.findUserRoles(page, size);
    }

    /**
     * 根据用户id查询用户角色对应关系
     *
     * @param userId
     * @return 用户角色对应关系集合
     */
    @Override
    public List<UserRole> getUserRolesByUserId(Long userId) {
        return userRuleDAO.findUserRoleByUserId(userId);
    }

    /**
     * 保存用户角色对应关系
     *
     * @param userId
     * @param roleIds
     */
    @Override
    public void addUserRole(Long userId, Long[] roleIds) {
        List<UserRole> userRoles = new ArrayList<>();
        for (int i = 0; i < roleIds.length; i++) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleIds[i]);
            userRoles.add(userRole);
        }
        userRuleDAO.saveUserRoles(userRoles);
    }

    @Override
    public List<LimitUser> getLimitUser(String username,int page,int size) {
        List<User> userList= (List<User>) userDAO.fingPage(page,size);
        LimitUser limitUser=null;
        User user= LoginUserCache.get(username);
        if(user==null||user.getId()==null){
            return null;
        }
        Long id=user.getId();
        List<LimitUser> limitUsers=new ArrayList<>();
        for(User u:userList){
            limitUser=new LimitUser();
            limitUser.setId(u.getId());
            limitUser.setName(u.getName());
            limitUser.setRoleName(getUserRoleNames(u.getId()));
            if(u.getId()>id) {
                limitUser.setFlag(true);
            }else{
                limitUser.setFlag(false);
            }
            limitUsers.add(limitUser);
        }
        return limitUsers;
    }

    private String getUserRoleNames(Long id){
        String roleName="";
        List<UserRole> userRoles=userRuleDAO.findUserRoleByUserId(id);
        for(UserRole userRole:userRoles){
            roleName+=roleDAO.findById(userRole.getRoleId()).getName()+",";
        }
        roleName.substring(0,roleName.length()-2);
        System.out.println("getUserRoleNames:roleName="+roleName);
        return roleName;
    }

    @Override
    public void updateUserRole(Long userId, Long[] roleIds) throws Exception {
        List<UserRole> userRoles=userRuleDAO.findUserRoleByUserId(userId);
        for(UserRole userRole:userRoles){
            for(int i=0;i<roleIds.length;i++){
                userRole.setRoleId(roleIds[i]);
            }
            userRuleDAO.updateUserRole(userRole);
        }
    }

    @Override
    public Integer getCount() {
        return userDAO.getCount();
    }
}

